package com.example.meterialproject.view.behavior.demo1

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.os.Parcelable
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import androidx.core.os.bundleOf
import androidx.core.view.ViewCompat
import com.example.meterialproject.bean.OffSet
import com.example.meterialproject.exts.TAG
import com.example.meterialproject.exts.dp
import com.example.meterialproject.view.behavior.demo1.MoveBehavior.Companion.PARCELABLE
import com.example.meterialproject.view.behavior.demo1.MoveBehavior.Companion.TEXT_FLOAT
import com.example.meterialproject.view.behavior.demo1.MoveBehavior.Companion.TEXT_INT
import com.example.meterialproject.view.behavior.demo1.MoveBehavior.Companion.TEXT_STRING
import com.example.meterialproject.view.behavior.demo1.MoveBehavior.Companion.TEXT_STRING_LIST

/**
 *
 * @ClassName: MoveView
 * @Author: android 超级兵
 * @CreateDate: 4/12/22$ 4:37 PM$
 */
class MoveView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var lastOffset = OffSet(0f, 0f)

    /*
     * 作者:android 超级兵
     * 创建时间: 4/13/22 10:11 AM
     * TODO 4. 如果view显示就开始测量 (tips:会调用多次,至少2次)
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthSize = customMeasure(widthMeasureSpec)
        val heightSize = customMeasure(heightMeasureSpec)
        Log.i(
            TAG, "onMeasure:width:${widthSize}" +
                    "\theight:${heightSize}"
        )
        setMeasuredDimension(widthSize, heightSize)
    }

    private fun customMeasure(measureSpec: Int) = let {
        val mode = MeasureSpec.getMode(measureSpec)
        val size = MeasureSpec.getSize(measureSpec)
        when (mode) {
            MeasureSpec.UNSPECIFIED -> {
                100.dp(context).toInt()
            }
            MeasureSpec.EXACTLY -> {
                size
            }
            MeasureSpec.AT_MOST -> {
                100.dp(context).toInt()
            }
            else -> 0
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                lastOffset = OffSet(event.x, event.y)
            }
            MotionEvent.ACTION_MOVE -> {
                val dx = event.x - lastOffset.x
                val dy = event.y - lastOffset.y

                // 方式一
                ViewCompat.offsetLeftAndRight(this, dx.toInt())
                ViewCompat.offsetTopAndBottom(this, dy.toInt())

                // 方式二
//                layout(
//                    (left + dx).toInt(),
//                    (top + dy).toInt(),
//                    (right + dx).toInt(),
//                    (bottom + dy).toInt()
//                )
            }
        }
        return true
    }


    /*
     * 作者:android 超级兵
     * 创建时间: 4/22/22 4:05 PM
     * TODO View 保存状态
     *
     * tips: 一定要给控件设置id!!
     */
    override fun onSaveInstanceState(): Parcelable {
        return bundleOf(
            TEXT_STRING to "点击关注不迷路,",
            TEXT_INT to 10086,
            TEXT_FLOAT to 77.77f,
            TEXT_STRING_LIST to arrayListOf("s", "z", "j"),

            // 重点!
            PARCELABLE to super.onSaveInstanceState(),
        )
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 4/22/22 4:05 PM
     * TODO View获取状态
     */
    override fun onRestoreInstanceState(state: Parcelable?) {
        (state as Bundle).apply {
            super.onRestoreInstanceState(getParcelable(PARCELABLE))
            Log.i(
                TAG,
                "onRestoreInstanceState:"
                + getString(TEXT_STRING)
                + "\t" + getInt(TEXT_INT)
                + "\t" + getFloat(TEXT_FLOAT)
                + "\t" + getStringArrayList(TEXT_STRING_LIST)
            )
        }
    }
}